Add limit iterator and context plumbing for --limit flag#4724
Add limit iterator and context plumbing for --limit flag#4724simonfaltum wants to merge 3 commits intomainfrom
Conversation
|
Commit: 1fbc876
16 interesting tests: 7 SKIP, 6 RECOVERED, 3 flaky
Top 20 slowest tests (at least 2 minutes):
|
3c4f97c to
8fb9c37
Compare
4b7a713 to
8c45e01
Compare
8c45e01 to
b9184da
Compare
d0812ba to
67aaf00
Compare
shreyas-goenka
left a comment
There was a problem hiding this comment.
Note: This review was posted by Claude (AI assistant). Shreyas will do a separate, more thorough review pass.
Priority: LOW — Clean, well-structured PR
MEDIUM: Next() doesn't guard against remaining <= 0
limitIterator.Next() unconditionally delegates to inner.Next() without checking remaining > 0. If Next() is called without first checking HasNext(), the limit won't be enforced (remaining goes negative). Low severity since all callers use the HasNext()/Next() pattern, but a guard would be cheap:
func (i *limitIterator[T]) Next(ctx context.Context) (T, error) {
if i.remaining <= 0 {
var zero T
return zero, fmt.Errorf("iterator exhausted")
}
// ...
}What looks good
WithLimit/GetLimitfollows standard Go context value patternApplyLimit(ctx, iter)keeps call sites cleanlimitIteratoris unexported (correct encapsulation)- Limit of 0 means "no limit" (common CLI convention)
- Thorough table-driven tests covering edge cases
- Lazy evaluation preserves iterator semantics
Overall excellent PR. The Next() guard is the only actionable item.
Return listing.ErrNoMoreItems when Next() is called with remaining <= 0, so the limit is enforced even if the caller skips HasNext().
pietern
left a comment
There was a problem hiding this comment.
I think this should be implemented in https://github.com/databricks/databricks-sdk-go/blob/main/listing/listing.go
It seems backwards to propagate the limit flag via the context and apply it only when rendering, when the callsite can wrap the response in listing.LimitN(iterator, limit) (hypothetical) and everything else works out of the box.
Why
The codegen template generates a --limit flag on every paginated list command. That generated code calls cmdio.WithLimit(ctx, n) to store the limit in context. RenderIterator then applies a limit iterator to cap total results. This PR adds the runtime plumbing that the generated code depends on.
Changes
Before: no mechanism to cap total results from list commands.
Now: libs/cmdio provides a limit iterator (wraps a listing.Iterator and stops after N items), WithLimit/GetLimit (context key for the limit value), and an ApplyLimit helper. All three RenderIterator functions call ApplyLimit before rendering.
Implementation:
Test plan